<template>
  <div class="login_container">
    <div class="login_box" style="height: 300px" v-if="isLogin">
      <h1
        @click="
          isLogin = !isLogin;
          resetForm();
        "
        style="color: #999999; cursor: pointer"
      >
        Login
      </h1>
      <el-form :model="form" status-icon ref="form" :rules="rules">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            prefix-icon="el-icon-user"
            placeholder="用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="form.password"
            prefix-icon="el-icon-lock"
            placeholder="密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="form.code"
            prefix-icon="el-icon-key"
            placeholder="验证码"
          >
            <template #append>
              <img
                @click="getCaptcha"
                style="height: 40px; width: 140px"
                :src="form.base64"
                alt=""
              />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item class="el-form-item__content">
          <el-button
            type="primary"
            size="small"
            style="width: 100%"
            @click="submitForm"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="login_box" style="height: 360px" v-else>
      <h1
        style="color: #999; cursor: pointer"
        @click="
          isLogin = !isLogin;
          resetForm();
        "
      >
        Register
      </h1>
      <el-form :model="form" status-icon ref="form" :rules="rules">
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            prefix-icon="el-icon-user"
            placeholder="用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="form.password"
            prefix-icon="el-icon-lock"
            placeholder="密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="passwordAgain">
          <el-input
            type="password"
            v-model="form.passwordAgain"
            prefix-icon="el-icon-lock"
            placeholder="确认密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="form.code"
            prefix-icon="el-icon-key"
            placeholder="验证码"
          >
            <template #append>
              <img
                @click="getCaptcha"
                style="height: 34px; width: 100px"
                :src="form.base64"
                alt=""
              />
            </template>
          </el-input>
        </el-form-item>
        <el-form-item class="el-form-item__content">
          <el-button
            size="small"
            style="width: 100%"
            type="primary"
            @click="submitForm"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      isLogin: true,
      form: {
        isAdmin: 1,
        username: "15173067340",
        password: "20020123feng",
        passwordAgain: "",
        code: "",
        key: "",
        base64: "",
      },
      rules: {
        username: [{ required: true, message: "用户名必填", trigger: "blur" }],
        password: [{ required: true, message: "密码必填", trigger: "blur" }],
        passwordAgain: [
          { required: true, message: "二次密码必填", trigger: "blur" },
          { validator: this.validateConfirmPassword, trigger: "blur" },
        ],
        code: [{ required: true, message: "验证码必填", trigger: "blur" }],
      },
      token: "",
    };
  },
  mounted() {
    this.getCaptcha();
  },
  methods: {
    validateConfirmPassword(rule, value, callback) {
      if (value !== this.form.password) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    },
    resetForm() {
      this.$refs.form.resetFields();
    },
    async getCaptcha() {
      const { data } = await this.$http.get("/auth/captcha");
      this.form.base64 = data.captchaImg;
      this.form.key = data.key;
    },
    submitForm() {
      const arr = [
        {
          url: "/auth/login",
          message: "登录",
        },
        {
          url: "/auth/register",
          message: "注册",
        },
      ];
      this.$refs.form.validate(async (isVaild) => {
        if (isVaild) {
          let obj = {};
          if (this.isLogin) {
            obj = arr[0];
          } else {
            obj = arr[1];
          }
          const { data, status } = await this.$http.post(obj.url, this.form);
          console.log(status);
          if (status === 200) {
            this.$message({
              type: "success",
              message: `${obj.message}成功`,
              duration: 1000,
            });
            if (this.isLogin) {
              window.sessionStorage.setItem("token", data.token);
              this.$router.push("/");
            } else {
              this.isLogin = true;
              this.form.code = "";
            }
          } else {
            this.$message.error("验证码错误");
            this.getCaptcha();
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #4c7cc3;
  .login_box {
    padding: 20px;
    width: 450px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    h1 {
      width: 100%;
      text-align: center;
    }
    .el-form-item__content {
      width: 100%;
    }
  }
}
</style>
